package com.classtwo.colleage.service.iml;

import com.classtwo.colleage.conf.*;
import com.classtwo.colleage.model.*;
import com.classtwo.colleage.tool.StringToTimeStamp;
import com.classtwo.colleage.vo.LeavePaperVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class StudentServiceImpl {

    @Autowired private DatabaseStudentInfo studentInfo;
    @Autowired private DatabaseTeacherInfo teacherInfo;
    @Autowired
    private DatabaseUser_classInfo classInfo;
    @Autowired
    private DatabaseUser_attendanceInfo user_attendance;
    @Autowired
    private DatabasePublicTellInfo publicTellInfo;
    @Autowired
    private DatabaseLeavePaperInfo leavePaperInfo;

    //登陆检测
    public Map<String, String> loginExam(Student student) {
        Map<String,String> map=new HashMap<String, String>();
        List<Student> list = studentInfo.findByUsernameAndPassword(student.getUsername(),student.getPassword());
        if(list.size() !=0){

            map.put("Answer","TRUE");
        }
        else {
            map.put("Answer","FALSE");
        }
        return map;
    }
    //注册账号重复检测
    public Map<String,String> loginRegisterExam(Student data ){
        Map<String,String> map=new HashMap<String, String>();

        if(studentInfo.findByUsername(data.getUsername()).size() ==0 && teacherInfo.findByUsername(data.getUsername()) == null){
            map.put("Answer","TRUE");
        }
        else {
            map.put("Answer", "FALSE");
        }
        return map;
    }
    //学生登陆界面
    public Student studentLoginUser(String username){
        List<Student> list = studentInfo.findByUsername(username);
        Student student = list.get(0);
        return student;
    }
    //学生注册
    public  void registerStudent(Student student){
        studentInfo.save(student);
    }
    //刷新列表
    public List<User_class> studentClassNext(String username){
        List<User_class> list= classInfo.findByUsername(username);
        return list;
    }
    //离开课堂
    public void studentExitClass(String classnode,String username){
        User_class user_class = classInfo.findByUsernameAndClassnode(username,classnode);
        classInfo.delete(user_class);
        //清空考勤记录
        List<User_attendance> user = user_attendance.findAllByUsernameAndClassnode(username,classnode);
        for(User_attendance data:user){
            user_attendance.delete(data);
        }
        //数量减一
        List<User_class> list = classInfo.findAllByClassnode(classnode);
        for(User_class classdata:list){
            //班级人数减1
            classdata.setClassnumber(classdata.getClassnumber()-1);
            classInfo.save(classdata);
        }
    }
    //加入班级
    public  Map<String,String> studentEnterClass(String classnode,String username,String name,String studentnumber,String classnode1){
        Map<String,String> map = new HashMap<>();
        User_class user_class = classInfo.findByClassnodeAndUsertype(classnode,"老师");
        //判断是否重复
        User_class user_class1=classInfo.findByUsernameAndClassnode(username,classnode);
        //判断是否存在班级
        if(user_class == null){
            map.put("Answer","NULL");
        }
        else {
            //判断是否重复
            if(user_class1 !=null){
                map.put("Answer","Repeat");
            }
            else {
                User_class data = new User_class();
                data.setUsername(username);
                data.setClassnode(classnode);
                data.setClassdesn(user_class.getClassdesn());
                data.setUsertype("学生");
                data.setClassname(user_class.getClassname());
                data.setName(name);
                data.setTeachername(user_class.getTeachername());
                data.setClassnumber(user_class.getClassnumber());
                classInfo.save(data);
                //班级数量加一
                List<User_class> list = classInfo.findAllByClassnode(classnode);
                for(User_class classdata:list){
                    //班级人数加1
                    classdata.setClassnumber(classdata.getClassnumber()+1);
                    classInfo.save(classdata);
                }
                //考勤默认设置

                User_class FirstData =classInfo.findFirstByClassnodeAndUsertype(classnode,"学生");
                //1.取出班级考勤中日期的数量
                List<User_attendance> list1 =user_attendance.findAllByUsernameAndClassnode(FirstData.getUsername(),classnode1);
                if(list1.size() == 0){
                    for(int i = 0;i< 4;i++) {
                        //当前时间
                        String TimeNow = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
                        User_attendance user_data = new User_attendance();
                        user_data.setAttendance((i+1)+TimeNow);
                        user_data.setClassnode(classnode);
                        user_data.setName(name);
                        user_data.setStatus("1");
                        user_data.setStudentnumber(studentnumber);
                        user_data.setUsername(username);
                        user_attendance.save(user_data);
                    }
                }else {
                    for(User_attendance data3 : list1) {
                        User_attendance user_data = new User_attendance();
                        user_data.setAttendance(data3.getAttendance());
                        user_data.setClassnode(data3.getClassnode());
                        user_data.setName(name);
                        user_data.setStatus("1");
                        user_data.setStudentnumber(studentnumber);
                        user_data.setUsername(username);
                        user_attendance.save(user_data);
                    }
                }
                //classInfo.save(user_class);
                map.put("Answer","success");
            }
        }
        return map;
    }
    //进入班级考勤界面--记录班级名
    public User_class recordClassName(String classnode){
        User_class user_class = classInfo.findFirstByClassnode(classnode);
        return user_class;
    }
    //进入班级考勤界面--考勤stuNumber数据获取
    public String[] getAttendanceStuNumberData(String classnode){
         List<String> attendance = new ArrayList<>();
         List<String> user =  new ArrayList<>();
        //考勤数据获取
        //1、获取班级人数全部数量，除了老师
        List<User_class> list = classInfo.findAllByUsertypeAndClassnode("学生",classnode);
        int member_number = list.size();
        //3.create all_data 素有数据
        String all_data[][] = new String[member_number][100];
        String[] stuNumber = new String[100];
        user = new ArrayList<>();
        attendance =  new ArrayList<>();
        //2.all student information
        int i = -1,j=-1;
        for(User_class data:list){
            //获取名字
            user.add(data.getName());
            //查找学号信息,如果是老师身份听课则用账号
            if(studentInfo.findAllByNameAndUsername(data.getName(),data.getUsername()) == null){
                stuNumber[i]=data.getUsername();
            }else {
                stuNumber[i]=studentInfo.findAllByNameAndUsername(data.getName(),data.getUsername()).getStudentnumber();
            }
        }
        return stuNumber;
    }
    //进入考勤界面-考勤all_data数据获取
    public String[][] getAttendanceAll_dataData(String classnode,String classnode1){
        List<String> attendance = new ArrayList<>();
        //考勤数据获取
        //1、获取班级人数全部数量，除了老师
        List<User_class> list = classInfo.findAllByUsertypeAndClassnode("学生",classnode);
        int member_number = list.size();
        //3.create all_data 素有数据
        String all_data[][] = new String[member_number][100];

        attendance =  new ArrayList<>();
        //2.all student information
        int i = -1,j=-1;
        for(User_class data:list){
            i++;
            List<User_attendance> attendance_data = user_attendance.findAllByUsernameAndClassnode(data.getUsername(),classnode1);
            //得到考勤的所有日期
            if(i == 0){
                for(User_attendance data1: attendance_data){
                    attendance.add(data1.getAttendance());
                }
            }
            //得到每个用户不同日期的不同状态
            for(User_attendance data1: attendance_data){
                j++;
                all_data[i][j]=data1.getStatus();
            }
            j=-1;
        }
        return all_data;
    }

    //成员界面
    public List<User_class> jumpMember(String classnode){
        List<User_class> allClass = classInfo.findAllByUsertypeAndClassnode("学生",classnode);
        return allClass;
    }

    //通知界面
    public List<Publictell> jumpPublicTell(String classnode){
        List<Publictell> publictells = publicTellInfo.findByClassnode(classnode);
        return publictells;
    }

    //学生特殊情况-请假界面
    public Student jumpSpecialcase(String username){
        Student student = studentInfo.findFirstByUsername(username);
        return student;
    }

    //提交请假条界面
    public void saveLeavePaper(String classnode,String leave_byone,String leave_byone_number,String leave_info,String start_time,String end_time,String creat_time,String teacherName){
        Leave_paper leave_paper = new Leave_paper();
        leave_paper.setLeave_byone(leave_byone);
        leave_paper.setLeave_info(leave_info);
        leave_paper.setClassnode(classnode);
        leave_paper.setTeacherName(teacherName);
        leave_paper.setStudentNumber(leave_byone_number);
        leave_paper.setStart_time(StringToTimeStamp.str2TimeStamp(start_time));
        leave_paper.setCreat_time(StringToTimeStamp.str2TimeStamp(creat_time));
        leave_paper.setEnd_time(StringToTimeStamp.str2TimeStamp(end_time));
        leave_paper.setStatus("未审核");

        leavePaperInfo.save(leave_paper);
    }
    //查看请假条记录
    public LeavePaperVo findStuLeaveList (String username,String classnode){
        LeavePaperVo leavePaperVos = new LeavePaperVo();
        List<Leave_paper> leave_paper =new ArrayList<>();
        Student student = studentInfo.findFirstByUsername(username);
        leave_paper= leavePaperInfo.findByStudentNumberAndClassnode(student.getStudentnumber(),classnode);

        if(leave_paper.size()==0){
            /*publictellVo.setPublictell(publictells);*/
            /*   leavePaperVos.add(new LeavePaperVo(leave_paper,false));*/
            leavePaperVos.setIsdata(false);
            leavePaperVos.setLeave_papers(leave_paper);
        }
        if(leave_paper.size()!=0){
            leavePaperVos.setIsdata(true);
            leavePaperVos.setLeave_papers(leave_paper);
        }
        return leavePaperVos;
    }





}
